package net.csdn.business.kanban.api.mapper;

/**
 * @ClassName KanbanMapper
 * @Description TODO
 * @Author liuzhisheng
 * @Date 2023/9/13
 **/
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.csdn.business.kanban.api.entity.KanbanDetail;
import net.csdn.business.kanban.common.domain.request.MoveIssueParam;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface KanbanDetailMapper extends BaseMapper<KanbanDetail> {

    @Select("select business_id from kanban_detail where kanban_id = #{kanbanId} and project_id = #{projectId} and business_type =#{businessType}")
    List<String> getIssueIds(@Param("projectId") Long projectId,
                           @Param("businessType") Integer businessType,
                           @Param("kanbanId") String kanbanId);

    @Delete("delete from kanban_detail where kanban_id = #{kanbanId}")
    int removeKanbanDetail(@Param("kanbanId") String kanbanId);

    @Update("UPDATE kanban_detail SET field_content_json = JSON_REMOVE(field_content_json, CONCAT('$.', '\"', #{fieldId}, '\"')) where kanban_id = #{kanbanId}")
    int removeFieldContentJson(@Param("kanbanId") String kanbanId, @Param("fieldId") Long fieldId);

    @Update("update kanban_detail set title=#{filedValue} where id=#{id}")
    int updateTitle(@Param("id")Long id, @Param("filedValue")String filedValue);

    @Update("update kanban_detail set assignee=#{filedValue} where id=#{id}")
    int updateAssignee(@Param("id")Long id, @Param("filedValue")String filedValue);

    @Update("update kanban_detail set labels=#{filedValue} where id=#{id}")
    int updateLabels(@Param("id")Long id, @Param("filedValue")String filedValue);

    @Update("update kanban_detail set milepost=#{filedValue} where id=#{id}")
    int updateMilepost(@Param("id")Long id, @Param("filedValue")String filedValue);

    @Update("<script>" +
            "UPDATE kanban_detail " +
            "SET project_id=#{param.projectId},business_id=#{param.nIssueId},serial_number=#{param.serialNumber}," +
            "assignee=null,labels=null,milepost=null " +
            "WHERE id IN " +
            "<foreach collection='ids' item='id' index='index' open='(' close=')' separator=','>" +
            "   #{id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateIssueDetail(@Param("ids")List<Long> ids, @Param("param")MoveIssueParam param);
}
